package com.cysyz.mylib.nestedconstructparser.impl;

//~--- JDK imports ------------------------------------------------------------

import com.cysyz.mylib.nestedconstructparser.Tag;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * sdfsdf description
 *
 *
 * @version        0.2, 14/08/21
 * @author         Cui Yong   cysyz1229@gmail.com
 */
public class BaseNestedconstructParser implements com.cysyz.mylib.nestedconstructparser.NestedconstructParser {

    /**
     * Method description
     *
     *
     * @param data
     * @param targetTags 要搜集的标签的svc，格式为开始标签，结束标签，开始标签，结束标签，开始标签。
     *                   如"<s:if[\\w\\W]*?>", "</s:if>", "<s:elseif[\\w\\W]*?>", "</s:elseif>", "<s:else[\\w\\W]*?>", "</s:else>"
     *
     * @return 要搜集的标签的集合
     */
    @Override
    public List<Tag> parse(String data, String... targetTags) {

        // 存放起始节点，用于和结束节点匹配
        Stack<Tag> work = new Stack<Tag>();

        // 保存所有起始和结束节点
        List<Tag> allTags = new ArrayList<Tag>();
        List<Pattern> targetTagPatterns = new ArrayList<Pattern>();

        for (String tarTag : targetTags) {
            targetTagPatterns.add(Pattern.compile(tarTag));
        }

        // 在元字符前加转义符
        // String nstag = stag.replaceAll("([\\!\\:\\*\\.\\+\\(\\]\\[\\?\\{\\}\\^\\$\\|\\\\])", "\\\\$1");
        // String netag = etag.replaceAll("([\\!\\:\\*\\.\\+\\(\\]\\[\\?\\{\\}\\^\\$\\|\\\\])", "\\\\$1");
        // 创建总的匹配表达式
        StringBuilder regBuilder = new StringBuilder("(");

        for (String tarTag : targetTags) {
            regBuilder.append("(?:");
            regBuilder.append(tarTag);
            regBuilder.append(")");
            regBuilder.append("|");
        }

        deleteLastChar(regBuilder);
        regBuilder.append(")");

        Pattern p = Pattern.compile(regBuilder.toString(), Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
        Matcher m = p.matcher(data);
        int deep = 0;
        Stack<Integer> toMatchPattern = new Stack<Integer>();

        while (m.find()) {
            String matchedValue = m.group(0);

            for (int i = 0; i < targetTagPatterns.size(); i++) {
                Pattern pattern = targetTagPatterns.get(i);

                if (pattern.matcher(matchedValue).matches()) {
                    if (i % 2 == 0) {
                        if (!toMatchPattern.isEmpty()
                                && targetTagPatterns.get(toMatchPattern.peek()).pattern().equals(pattern.pattern())) {
                            deep++;
                        }

                        toMatchPattern.push(i);
                    } else {
                        toMatchPattern.pop();
                        deep = toMatchPattern.size();
                    }

                    break;
                }
            }

            Tag tag = new Tag(matchedValue, m.start(), m.end(), deep);

            allTags.add(tag);
        }

        return allTags;
    }

    private StringBuilder deleteLastChar(StringBuilder stringBuilder) {
        return stringBuilder.deleteCharAt(stringBuilder.length() - 1);
    }


    /**
     * Method description
     *
     *
     * @param args
     */
    public static void main(String[] args) {
        Stack<String> stack = new Stack<String>();

        stack.push("www");
        stack.push("sss");
        System.out.println(stack.peek());
    }

}


//~ Formatted by Jindent --- http://www.jindent.com
